let index = 0;
let a = {
    value: "a",
    valueOf() {
        return ["a", "b", "c"][index++];
    },
};
console.log(a + a);
// let a = {
//     value: "a",
//     valueOf() {
//         return ["a"][0]
//     }
// }
// console.log(a);

/**
 * 解法1: Object.defineProperty() 外部变量
 */
let value = "a";
Object.defineProperty(this, "a", {
    get() {
        let result = value;
        if (value === "a") {
            value = "b";
        } else if (value === "b") {
            value = "c";
        }
        return result;
    },
});
console.log(a + a + a);
/**
 * 解法1(优化版)：Object.defineProperty() 内部变量
 */
Object.defineProperty(this, "a", {
    get() {
        this._v = this._v || "a";
        if (this._v === "a") {
            this._v = "b";
            return "a";
        } else if (this._v === "b") {
            this._v = "c";
            return "b";
        } else {
            return this._v;
        }
    },
});
console.log(a + a + a);

/**
 * 解法2: Object.prototpye.valueOf()
 */
let index = 0;
let a = {
    value: "a",
    valueOf() {
        return ["a", "b", "c"][index++];
    },
};
console.log(a + a + a);

/**
 * 解法3：charCodeAt，charFromCode
 */
let code = "a".charCodeAt(0);
let count = 0;
Object.defineProperty(this, "a", {
    get() {
        let char = String.fromCharCode(code + count);
        count++;
        return char;
    },
});
console.log(a + a + a); // 'abc'

/**
 * 解法3(优化版一)：内部变量this._count和_code
 */
Object.defineProperty(this, "a", {
    get() {
        let _code = "a".charCodeAt(0);
        this._count = this._count || 0;
        let char = String.fromCharCode(_code + this._count);
        this._count++;
        return char;
    },
});
console.log(a + a + a); // 'abc'

/**
 * 解法3(优化版二)：内部变量this._code
 */
Object.defineProperty(this, "a", {
    get() {
        this._code = this._code || "a".charCodeAt(0);
        let char = String.fromCharCode(this._code);
        this._code++;
        return char;
    },
});
console.log(a + a + a); // 'abc'

/*
 题目扩展: 打印`a...z`
 a+a+a; //'abc'
 a+a+a+a; //'abcd'
*/
/**
 * charCodeAt，charFromCode
 */
let code = "a".charCodeAt(0);
let count = 0;
Object.defineProperty(this, "a", {
    get() {
        let char = String.fromCharCode(code + count);
        if (count >= 26) {
            return "";
        }
        count++;
        return char;
    },
});
// 打印‘abc’
console.log(a + a + a); // 'abc'

// 打印‘abcd’
let code = "a".charCodeAt(0);
let count = 0;
// {...定义a...}
console.log(a + a + a); // 'abcd'

// 打印‘abcdefghijklmnopqrstuvwxyz’
let code = "a".charCodeAt(0);
let count = 0;
// {...定义a...}
let str = "";
for (let i = 0; i < 27; i++) {
    str += a;
}
console.log(str); // "abcdefghijklmnopqrstuvwxyz"

/*
 题目扩展（优化版）: 打印`a...z`
 a+a+a; //'abc'
 a+a+a+a; //'abcd'
*/

Object.defineProperty(this, "a", {
    get() {
        this._code = this._code || "a".charCodeAt(0);
        let char = String.fromCharCode(this._code);
        if (this._code >= "a".charCodeAt(0) + 26) {
            return "";
        }
        this._code++;
        return char;
    },
});
// 打印‘abc’
console.log(a + a + a); // 'abc'
